﻿using System.ComponentModel.Composition;
using NthDownload.Common.ViewModels;
using NthDownload.Common.Mapping;
using NthDownload.DownloadPlugins.Http.Persistence;

namespace NthDownload.DownloadPlugins.Http.ViewModels
{
    public class HttpSettingsViewModel : ViewModelConductorBase
    {
        #region Constructors

        public HttpSettingsViewModel()
        {
            Settings = (HttpSettings)ObjectMappingService.Clone(CurrentSettings);
            ShowGeneralSettings();
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Current Settings
        /// </summary>
        [Import(AllowRecomposition = true)]
        public HttpSettings CurrentSettings { get; set; }

        /// <summary>
        /// Settings
        /// </summary>
        [Import(AllowRecomposition = true)]
        public HttpSettingsPersistenceService SettingsPersistenceService { get; set; }

        /// <summary>
        /// Settings
        /// </summary>
        public HttpSettings Settings { get; set; }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Shows general settings
        /// </summary>
        public void ShowGeneralSettings()
        {
            ActivateItem(new HttpGeneralSettingsViewModel(Settings));
        }

        /// <summary>
        /// Shows credential settings
        /// </summary>
        public void ShowCredentialSettings()
        {
            ActivateItem(new HttpCredentialsSettingsViewModel(Settings));
        }

        /// <summary>
        /// Save
        /// </summary>
        public void Save()
        {
            ObjectMappingService.Update(Settings, CurrentSettings);
            SettingsPersistenceService.Save();
            TryClose();
        }

        /// <summary>
        /// Cancel
        /// </summary>
        public void Cancel()
        {
            TryClose();
        }

        #endregion Methods
    }
}
